package cn.fl.insurance.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.insurance.dto.InsPlcBscInfoDTO;
import cn.lb.framework.poi.excel.ExcelExportUtil;
import cn.lb.framework.poi.excel.entity.TemplateExportParams;
import cn.lb.framework.poi.excel.entity.enmus.ExcelStyleType;

/**
 * Function: 保险导出工具类
 * Created by Administrator on 2016/9/22.
 */
public class ExportInsuranceList {

	private static final Logger logger = LoggerFactory.getLogger(ExportInsuranceList.class);

    /**
     * 导出结算保单
     * @param map
     * @return
     */
    public ByteArrayOutputStream exportInsStlListXls(Map<String, Object> map) {
        ByteArrayOutputStream baos = null;//¥
        List<InsPlcBscInfoDTO> list = (List<InsPlcBscInfoDTO>) map.get("insStlList");
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
        Long ttlFee=0L;//保费总金额
        Long ttlStlFee=0L;//总结算金额
        for (InsPlcBscInfoDTO dto : list) {
            ttlFee+=dto.getInsTtlFee();
            ttlStlFee+=(dto.getStlAmt()==null?0l:dto.getStlAmt());
            dto.setInsTtlAmts(nf.format(Double.valueOf(dto.getInsTtlFee())/100));
            if(null!=dto.getPlcBgTm()){
                dto.setPlcBgTms(DateUtils.formatDateToString(dto.getPlcBgTm(),DateUtils.YYYY_MM_DD));
            }
            if(StringUtils.isNotEmpty(dto.getStlAmt())){
                dto.setStlAmts(nf.format(Double.valueOf(dto.getStlAmt())/100));
            }
        }
        map.put("total",list.size());//总条数
        map.put("ttlFee",nf.format(Double.valueOf(ttlFee) / 100));//保费总金额
        map.put("ttlStlFee",nf.format(Double.valueOf(ttlStlFee) / 100));//总结算金额
        map.put("insStlList",list);
        TemplateExportParams params = new TemplateExportParams(ExportInsuranceList.class.getClassLoader().getResource("/template/service/insurance/insStlTemp.xls").getFile(), false, "结算保单");
        params.setHeadingRows(0);
        params.setHeadingStartRow(1);
        params.setStyle(ExcelStyleType.BORDER.getClazz());
        params.setColForEach(true);//注：包含遍历list
        try {
            Workbook book = ExcelExportUtil.exportExcel(params, map);
            baos = new ByteArrayOutputStream();
            book.write(baos);
        } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        }
        return baos;
    }

}
